package kaist.sep521.lego.circlebicycle;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CircleActivity extends Activity {
	private ArrayList<String> groupList = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_circle);

		Button exitCircle_button = (Button)findViewById(R.id.circle_exit_button);
		Button newCircle_button = (Button)findViewById(R.id.circle_newCircle);

		exitCircle_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				// request to exit group\
				String group_name = MainActivity.myInfo.getGroup();

				if( group_name!= null && group_name.equals("")){
					Toast.makeText(CircleActivity.this, "Please, Join a circle", Toast.LENGTH_SHORT).show();
				}else{
					circleExit();
					MainActivity.myInfo.setGroup("");
					TextView currentCircle = (TextView)findViewById(R.id.circle_current);
					currentCircle.setText("No Joined Circle");
					update();
				}

			}
		});

		newCircle_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				// move to make new group window
				Intent intent = new Intent(getBaseContext(), MakeGroupActivity.class);
				startActivity(intent);
			}
		});

		update();
	}


	@Override
	protected void onResume() {
		super.onResume();
		update();
	}

	public void update() {
		// Joined group name
		TextView currentCircle = (TextView)findViewById(R.id.circle_current);
		if( (MainActivity.myInfo.getGroup() == null) | (MainActivity.myInfo.getGroup() == "") )
			currentCircle.setText("No Joined Circle");
		else
			currentCircle.setText(MainActivity.myInfo.getGroup());

		// group list
		if (groupList != null)
			groupList.clear();
		groupList = makeGroupList();
		showGroupList(groupList);
	}

	private void circleExit() {
		String user = MainActivity.myInfo.getID();
		String cexitUrl = "http://14.63.196.253/~lego/circle_exit.php?user=" + user;

		//Clear the Voice Tag on the server.
		try {
			URL url = new URL(cexitUrl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				Log.w("Circle EXIT", "HTTP_SEND_OK");

			} else {
				Toast.makeText(this, "Server connection error!!", Toast.LENGTH_SHORT).show();
			}
			conn.disconnect();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private ArrayList<String> makeGroupList() {
		ArrayList<String> list = new ArrayList<String>();
		// parse GroupList on Server

		try {
			String xml = "http://14.63.196.253/~lego/grouplist.php";
			URL url = new URL(xml);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				InputStream is = conn.getInputStream();

				SAXParserFactory spf = SAXParserFactory.newInstance();
				SAXParser sp = spf.newSAXParser();
				XMLReader xr = sp.getXMLReader();
				GroupListParserHandler gHandler = new GroupListParserHandler();
				xr.setContentHandler(gHandler);
				xr.parse(new InputSource(is));
				list = gHandler.getParsedData();
			} else {
				Toast.makeText(this, "Server connection error!!", Toast.LENGTH_SHORT).show();
			}
			conn.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}

	private void showGroupList(ArrayList<String> list) {
		ListView lv = (ListView) this.findViewById(R.id.circle_circleList);
		GroupArrayAdapter adapter = new GroupArrayAdapter(getApplicationContext(), R.layout.group_listitem, list);
		adapter.setActivity(this);
		lv.setAdapter(adapter);
	}
}
